# !/usr/bin/env python
# -*-coding:utf-8 -*-
"""
# Author     ：hu_cl
# Date       ：2022/3/4 14:42
# File       : win_ins_uins.py
# Description：助手安装，删除，功能
"""
import ctypes
import os
import platform
import re


def system_get_platform_info():
    sysplatform = platform.system().lower().strip()
    python_bit = platform.architecture()[0]
    python_bit_num = re.findall('(\d+)\w*', python_bit)[0]
    return sysplatform, python_bit_num


sys_platform, python_bit_num = system_get_platform_info()


def load_lib(libpath):
    if python_bit_num == '32':
        if sys_platform == "linux":
            libpath = libpath.replace('\\', '/')
            c_lib = ctypes.cdll.LoadLibrary(libpath)
            code, msg = 200, c_lib
        elif sys_platform == "windows":
            c_lib = ctypes.windll.LoadLibrary(libpath)
            code, msg = 200, c_lib
        else:
            code, msg = 0, None
    else:
        code, msg = 0, None

    return code, msg


class InsNdis:
    path = os.getcwd()
    pathnow = os.path.abspath('InsNdis.dll')

    def __init__(self):
        self._p_ins_unins = None
        self.loadlib(InsNdis.pathnow)

    def loadlib(self, lib_path):
        code, c_lib_handle = load_lib(lib_path)
        if code == 200:
            # 助手的安装 卸载  true: 安装；False：卸载。
            self._p_ins_unins = c_lib_handle.CMD
            self._p_ins_unins.argtypes = (ctypes.c_bool, ctypes.c_char_p)
            self._p_ins_unins.restype = ctypes.c_bool
        else:
            print('系统环境是win32')

    @property
    def p_ins_unins(self):
        return self._p_ins_unins


class InsNdisNet:
    def __init__(self):
        self.libpcappolicy = InsNdis()

    def install(self, filename):
        """
        :param filename: 助手安装路径
        :return: True 安装正常 ，False 安装失败
        """
        path = filename.encode('utf-8')
        res = self.libpcappolicy.p_ins_unins(1, path)
        return res

    def uninstall(self, filename):
        """
        :param filename: 助手安装路径
        :return: True 卸载成功， False 卸载失败
        """
        path = filename.encode('utf-8')
        res = self.libpcappolicy.p_ins_unins(0, path)
        return res


if __name__ == '__main__':
    meth_c_lib = InsNdisNet()

    exit(0)
